package com.shire.shire_interface.controller.stat;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.common.bin.Response;
import com.shire.shire_interface.controller.BaseController;
import com.shire.shire_interface.model.stat.CsSessionStatExample;
import com.shire.shire_interface.service.stat.CsSessionStatService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

//@Api(tags = "统计数据")
@RestController
@RequestMapping("/stat")
public class CsSessionStatController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(CsSessionStatController.class);

    @Resource
    private CsSessionStatService sessionStatService;

//    @ApiOperation(value = "获取会话统计，参数以json body post. accounts:帐号列表， startDate, endDate")
    @ResponseBody
    @PostMapping("/getSessionStat")
    public Response getSessionStat(@RequestBody JSONObject bodyJson, HttpSession session){
        Response response = new Response();

        try{
            if (!checkLoginUser(session)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            List<String> accounts = new ArrayList<>();
            for (Object o : bodyJson.getJSONArray("accounts")) {
                accounts.add((String)o);
            }

            CsSessionStatExample example = new CsSessionStatExample();
            CsSessionStatExample.Criteria criteria = example.createCriteria();
            criteria.andWxaccountIn(accounts);

            if (bodyJson.containsKey("startDate")) {
                String startDate = bodyJson.getString("startDate");
                criteria.andDtGreaterThanOrEqualTo(startDate);
            }
            if (bodyJson.containsKey("endDate")) {
                String endDate = bodyJson.getString("endDate");
                criteria.andDtLessThanOrEqualTo(endDate);
            }

            response.setData(sessionStatService.getStatForAccountsByDate(example));
            response.success("操作成功");

        }catch (Exception e){
            e.printStackTrace();
            response.error("操作失败");
        }

        return response;
    }
}
